const WXAPI = require('apifm-wxapi')
const CONFIG = require('../../config.js')
//获取应用实例
var app = getApp();
Page({
  data: {
    sjc: Date.parse(new Date()),
    banners: [],
    jindex: 0,
    all: 0,
    touchStartY: 0, // 用于记录触摸开始的 Y 坐标
    isSwiping: false, // 防止多次触发
    tan_wx: false,
    tan_tel: false,
    tan_map: false,
    tan_need: false,
    id: 0,
    index: 0,
    need_tel: '',
    need_text: '',

  },
  async need() {
    let that = this;
    if (!that.data.need_tel) {
      wx.showToast({
        title: '请输入电话',
        icon: 'none',
        duration: 2000
      })
      return;
    } else {
      const res = await WXAPI.addComment({
        token: wx.getStorageSync('token'),
        type: 5,
        content: that.data.id + ":" + that.data.need_tel+' '+that.data.banners.need[that.data.index] + "," + that.data.need_text,
        mobile: that.data.need_tel
      })
      if (res.code == 0) {
        wx.showToast({
          title: '提交成功',
        })
        setTimeout(() => {
          that.setData({
            need_tel: '',
            need_text: '',
          })
          that.close();
        }, 1000);
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }


    }
  },
  bindPickerChange(e) {
    this.setData({
      index: e.detail.value
    })
  },
  bindInput(e) {
    this.setData({
      need_tel: e.detail.value
    })
  },

  bindTextAreaInput(e) {
    this.setData({
      need_text: e.detail.value
    })
  },

  copy_wx() {
    console.log("copy_wx", this.data.banners.wx);
    wx.setClipboardData({
      data: this.data.banners.wx,
      success: (res) => {
        wx.showToast({
          title: '复制成功',
          icon: 'success',
          duration: 2000
        })
        this.close();
      }
    })
  },
  tel() {
    wx.makePhoneCall({
      phoneNumber: this.data.banners.tel,
    })
  },
  map() {
    console.log("map", this.data.banners.map_jwd, this.data.banners.map_name, this.data.banners.map_addr);
    // wx.getLocation({
    //   type: 'gcj02', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
    //   success: function (res) {
    // console.log("map", res.latitude, res.longitude);
    wx.openLocation({
      latitude: this.data.banners.map_jwd[0] * 1,
      longitude: this.data.banners.map_jwd[1] * 1,
      scale: 10,
      name: this.data.banners.map_name,
      address: this.data.banners.map_addr,
      fail: function (e) {
        console.log(e)
      }
    })
    this.close()
    // }
    // })




  },

  onShareAppMessage() {
    console.log("ffff",this.data.id)
    // 自定义标题和封面图
    return {
      title: this.data.banners.share_title || 'mlito',
      path: '/pages/dm/dm?id=' + this.data.id,
      imageUrl: this.data.banners.share_img || 'https://www.mlito.com/wp-content/uploads/fx_zdy5_4.png',
    }
  },


  onShareTimeline() {
    console.log("ffff222",this.data.id)

    return {
      title: this.data.banners.share_title || 'mlito',
      query: 'id=' + this.data.id,
      imageUrl: this.data.banners.share_img || 'https://www.mlito.com/wp-content/uploads/fx_zdy5_4.png',
    }
  },
  // 记录触摸起点
  handleTouchStart(e) {
    this.setData({
      touchStartY: e.touches[0].clientY,
      isSwiping: true
    });
  },

  close() {
    this.setData({
      tan_wx: false,
      tan_tel: false,
      tan_map: false,
      tan_need: false
    })
  },

  // 处理滑动
  handleTouchMove(e) {
    const { touchStartY, isSwiping, jindex, all } = this.data;
    const touchMoveY = e.touches[0].clientY;
    const diff = touchMoveY - touchStartY;

    if (Math.abs(diff) > 30 && isSwiping) { // 判断滑动距离是否足够
      this.setData({ isSwiping: false }); // 锁定，防止重复触发

      if (diff < 0) {
        // 向上滑动
        if (jindex < all) {
          this.setData({ jindex: jindex + 1 });
        } else {
          this.setData({ jindex: 0 }); // 循环回到第一个
        }
      } else {
        // 向下滑动
        if (jindex > 0) {
          this.setData({ jindex: jindex - 1 });
        } else {
          this.setData({ jindex: all }); // 循环到最后一个
        }
      }
    }
  },


  up() {
    // console.log("up", this.data.jindex, this.data.all);
    if (this.data.jindex == 0) {
      // console.log("00");
      this.setData({
        jindex: this.data.all
      })
    }
    else if (this.data.jindex > 0) {
      // console.log("--");
      this.setData({
        jindex: this.data.jindex - 1
      })
    }
    console.log("jindex", this.data.jindex);
  },
  down() {
    // console.log("down", this.data.jindex, this.data.all);
    if (this.data.jindex == this.data.all) {
      // console.log("00");
      this.setData({
        jindex: 0
      })
    }
    else if (this.data.jindex < this.data.all) {
      // console.log("++");
      this.setData({
        jindex: this.data.jindex + 1
      })
    }

    console.log("jindex", this.data.jindex);
  },
  onShow: function () {
    var that = this;
  },
  async onLoad(e) {
    console.log("eeee",e);
    if(e.id){
    this.setData({
      id: e.id
    })
  }

  if(e.fxid){
    this.setData({
      id: e.fxid
    })
  }
    wx.setStorageSync('dm_id', e.id);


    let temp = {};
    const _this = this

    let res = await WXAPI.cmsPage(_this.data.id);
    if (res.code == 0) {
      temp.dm_img = (res.data.extJson.dm_img).split(";");
      temp.dm_wz = (res.data.extJson.dm_wz).split(",");
      if (res.data.extJson.dm_zdy) {
        temp.dm_zdy = (res.data.extJson.dm_zdy).split(",");
      }
      if (res.data.extJson.微信背景图) {
        temp.bj_wx = res.data.extJson.微信背景图;
        temp.wx = res.data.extJson.微信号;
      }
      temp.bj_tel = res.data.extJson.电话背景图;
      temp.bj_map = res.data.extJson.地址背景图;
      temp.bj_need = res.data.extJson.需求背景图;
      temp.map_jwd = (res.data.extJson.经纬度).split(",");
      temp.map_addr = res.data.extJson.地址;
      temp.map_name = res.data.extJson.名称;
      temp.tel = res.data.extJson.电话;
      temp.need = (res.data.extJson.需求).split(",");

      if (res.data.extJson.分享标题) {
        temp.share_title = res.data.extJson.分享标题;
      }
      if (res.data.extJson.分享图片) {
        temp.share_img = res.data.extJson.分享图片;
      }

    }



    _this.setData({
      banners: temp
    });

    console.log("temp", temp);


  },

  bt(e) {
    wx.vibrateShort();
    let temp = e.currentTarget.dataset.id;
    // 判断是纯数字的跳转到详情页，否则列表页
    if (temp.match(/^[0-9]*$/)) {
      wx.navigateTo({
        url: `/pages/xiangqing/index?id=${temp}`,
      })
    } else {
      wx.navigateTo({
        url: `/pages/taglist/index?id=${temp}`,
      });
    }
  },

  bts(e) {
    wx.vibrateShort();
    let temp = e.currentTarget.dataset.id;
    if (temp == 1) {
      // wx.navigateTo({
      //   url: '/pages/start/start'
      // })
      let temp = e.currentTarget.dataset.url;
      // 判断是纯数字的跳转到详情页，否则列表页
      if (temp.match(/^[0-9]*$/)) {
        wx.navigateTo({
          url: `/pages/xiangqing/index?id=${temp}`,
        })
      } else {
        wx.navigateTo({
          url: `/pages/taglist/index?id=${temp}`,
        });
      }
    }
    // 微信
    if (temp == 2) {
      if (this.data.banners.bj_wx) {
        this.setData({
          tan_wx: true
        })
      }
    }
    // 电话
    if (temp == 3) {
      this.setData({
        tan_tel: true
      })
    }
    // 导航
    if (temp == 4) {
      this.setData({
        tan_map: true
      })
    }
    // 需求
    if (temp == 5) {
      this.setData({
        tan_need: true
      })
    }
    // 好物
    if (temp == 6) {
      wx.switchTab({
        url: '/pages/haowu/index'
      })
    }
  },





  goToIndex: function (e) {
    let shopMod = wx.getStorageSync('shopMod')
    if (!shopMod) {
      shopMod = 0
    }
    if (app.globalData.isConnected) {
      wx.setStorage({
        key: 'app_show_pic_version',
        data: CONFIG.version
      })
      if (shopMod == 1) {
        wx.redirectTo({
          url: '/pages/shop/select',
        });
      } else {
        wx.switchTab({
          url: '/pages/index/index',
        });
      }
    } else {
      wx.showToast({
        title: '当前无网络',
        icon: 'none',
      })
    }
  }
});